#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 1e4 + 10, M = 1e7 + 10;
typedef long long ll;
ll f[M];
int w[N];
int t[N];
int n, m;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> t[i] >> w[i];
    }
    for (int i = 1; i <= m; i++)
    {
        for (int j = t[i]; j <= n; j++)
        {
            f[j] = max(f[j], f[j - t[i]] + w[i]);
        }
    }
    cout << f[n] << endl;
    return 0;
}